<?php


namespace App\Admin\Controllers\Chart;

// 会员统计控制器
use App\Models\Member;
use App\Models\RecordRecharge;
use Carbon\Carbon;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Rap2hpoutre\FastExcel\Facades\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;

class MemberChartController
{
	public function index(Content $content)
    {
    	$data = [
			'oneDay' => $this->Chart('day'),
			'oneMonth' => $this->Chart('month'),
			'oneYear' => $this->Chart('year')
		];
        return $content
	        ->header('会员统计')
	        ->title('会员统计')
	        //->row(view('admin.export.export-btn', ['route' => route('memberExport')]))
	        ->row(function (Row $row) use ($data){
        
				    $row->column(10, function (Column $column) use($data) {
				    	$rgb = [
				            'rgba(255,156,0, 0.5)',
						    'rgba(128,184,46, 0.5)',
						    'rgba(3,162,156, 0.5)',
					    ];
				    	$column->row(new Box('今日', view('admin.chart.member-chart', ['name' => 'oneDay', 'data' => $data['oneDay'], 'rgb' => $rgb])));
				    	
				    	$column->row(new Box('本月', view('admin.chart.member-chart', ['name' => 'oneMonth', 'data' => $data['oneMonth'], 'rgb' => $rgb])));
				    	
				        $column->row(new Box('今年', view('admin.chart.member-chart', ['name' => 'oneYear', 'data' => $data['oneYear'], 'rgb' => $rgb])));
				    });
				});
    }
    
    public function Chart($index = 'day')
    {
		$timeStart = new Carbon();
    	$timeEnd = new Carbon();
    	$timeBetween = [];
	    switch ($index) {
	    	case 'day':
	    		$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
	    		$raw = 'hour';
	    		$x = countX(24);
	    		break;
	    	case 'month':
	    		$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
	    		$raw = 'Day';
	    		$x = countX($timeStart->daysInMonth);
	    		break;
	    	case 'year':
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		$raw = 'Month';
	    		$x = countX(12);
	    		break;
	    	
	    	default:
	    		$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
	    		break;
	    }
        // x 轴为时间轴
    	$data[] = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 1)
		    ->selectRaw('sum(price) as price, '.$raw.'(created_at) as date')
		    ->groupBy('date')
		    ->get();
	    $data[] = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 2)
		    ->selectRaw('sum(price) as price, '.$raw.'(created_at) as date')
		    ->groupBy('date')
		    ->get();
	    $data[] = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 3)
		    ->selectRaw('sum(price) as price, '.$raw.'(created_at) as date')
		    ->groupBy('date')
		    ->get();
		//dd($x, $data);
    	foreach ($x as $k => $v)
	    {
			$yTemp[1][$k + 1] = ['x' => $k + 1, 'value' => 0];
			$yTemp[2][$k + 1] = ['x' => $k + 1, 'value' => 0];
			$yTemp[3][$k + 1] = ['x' => $k + 1, 'value' => 0];
			
	    	foreach ($data as $key => $value)
		    {
		        foreach ($value as $yIndex)
		        {
		        	if($yIndex['date'] == $v) {
		        	    $yTemp[$key + 1][$v]['value'] = $yIndex['price'];
			        }
		        }
		    }
	    }
    	$y['开通会员']['data'] = $yTemp[1];
    	$y['开通会员']['rgb'] = '';
    	$y['会员充值']['data'] = $yTemp[2];
    	$y['会员充值']['rgb'] = '';
    	$y['充值赠送']['data'] = $yTemp[3];
    	$y['充值赠送']['rgb'] = '';
    	return $y;
    }
    
    public function export(Request $request)
    {
    	$param = $request->all();
    	$validate = Validator::make($param, [
    		'startTime' => 'required',
    		'endTime' => 'required'
	    ]);
    	if($validate->failed()){
    		return false;
	    }
    	
        $time = new Carbon();
    	$timeStart = new Carbon($param['startTime']);
    	$timeEnd = new Carbon($param['endTime']);
    	$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
		//dd($this->exportData('year', $timeBetween));
    	return FastExcel::data($this->exportData('year', $timeBetween))
					->download('会员.' . $time->format('Y') . '年.xlsx');
    	
//		switch ($request->get('date')) {
//			case 'year':
//				return FastExcel::data($this->exportData('year'))
//					->download('会员.' . $time->format('Y') . '年.xlsx');
//				break;
//			case 'month':
//				return FastExcel::data($this->exportData('month'))
//					->download('会员.' . $time->format('Y-m') . '月.xlsx');
//				break;
//			case 'day':
//				return FastExcel::data($this->exportData('day'))
//					->download('会员.' . $time->format('Y-m-d') . '.xlsx');
//				break;
//			default :
//				return FastExcel::data($this->exportData('year'))
//					->download('会员.' . $time->format('Y') . '年.xlsx');
//				break;
//		}
    }
    
    public function exportData($index, $timeBetween)
    {
		switch ($index) {
			case 'day':
				$raw = 'Hour';
				$data = $this->collection($raw, $timeBetween);
				break;
			case 'month':
				$raw = 'Day';
				$data = $this->collection($raw, $timeBetween);
				break;
			case 'year':
				$raw = 'Month';
				$data = $this->collection($raw, $timeBetween);
				break;
		}
		
		return $data;
    }
    
    public function collection($raw, $timeBetween)
    {
    	switch ($raw){
			case 'Month':
				$as = '月份';
				break;
			case 'Day':
				$as = '日期';
				break;
			case 'Hour':
				$as = '时间';
				break;
		}
        // 开通会员
	    $memberCreate = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 1)
		    ->selectRaw($raw.'(created_at) as '.$as.' ,sum(price) as 金额')
		    ->groupBy($as)
		    ->get();
	    // 会员充值
	    $memberCharge = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 2)
		    ->selectRaw($raw.'(created_at) as '.$as.' ,sum(price) as 金额')
		    ->groupBy($as)
		    ->get();
	    // 充值赠送
	    $memberGive = RecordRecharge::whereBetween('created_at', $timeBetween)
		    ->Where('type', 3)
		    ->selectRaw($raw.'(created_at) as '.$as.' ,sum(price) as 金额')
		    ->groupBy($as)
		    ->get();

	    $data = new SheetCollection([
	    	'开通会员' => recordOrderChart($memberCreate),
		    '会员充值' => recordOrderChart($memberCharge),
		    '充值赠送' => recordOrderChart($memberGive)
	    ]);
	    
	    return $data;
    }
}